package cc.forgemod.customicon.client.texture;

import java.util.Collection;
import java.util.function.Function;

import cc.forgemod.customicon.common.icon.Icon;
import cc.forgemod.customicon.common.icon.IconManager;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;

public class CTextureGetter implements Function<ResourceLocation, TextureAtlasSprite> {

    public static final CTextureGetter INSTANCE = new CTextureGetter();

    @Override
    public TextureAtlasSprite apply(ResourceLocation pLoc) {
        if (pLoc instanceof CResourceLocation) {
            Icon tIcon = IconManager.getIcon(((CResourceLocation)pLoc).getIconKey());
            if (tIcon != null) return tIcon.getTexture();
        }

        IModel tMiss = ModelLoaderRegistry.getMissingModel();
        if (tMiss != null) {
            Collection<ResourceLocation> tTextures = tMiss.getTextures();
            if (!tTextures.isEmpty()) pLoc = tTextures.iterator().next();
        }

        return ModelLoader.defaultTextureGetter().apply(pLoc);
    }

}
